package com.perfect.controller;

import com.perfect.annotation.RequirePermission;
import com.perfect.base.BaseController;
import com.perfect.base.ResultInfo;
import com.perfect.query.BrandQuery;
import com.perfect.service.BrandService;
import com.perfect.vo.Brand;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RequestMapping("brand")
@Controller
public class BrandController extends BaseController {
    @Resource
    private BrandService brandService;

    //跳转至品牌列表展示页面
    @RequirePermission(code = "4040")
    @RequestMapping("index")
    public String brand(){
        return "brand/brand";
    }



//分页查询品牌列表
@RequirePermission(code = "404001")
    @RequestMapping("list")
    @ResponseBody
    public Map<String, Object> queryBrandList(BrandQuery brandQuery){
        return brandService.selectByParams(brandQuery);

    }





    /**
     *跳转至品牌添加页面
     */
    @RequirePermission(code = "404002")
    @RequestMapping("tobrandPage")
    public String tobrandPage(Integer brandId, HttpServletRequest request) {


//判断Id是否为空
        if(brandId != null){
            //用过id查询营销机会管理
            Brand brand  = brandService.selectByPrimaryKey(brandId);

            //将数据设置请求域中
            request.setAttribute("brand",brand);

        }

        return "brand/add_update";
    }


    //品牌添加
    @RequirePermission(code = "404003")
    @PostMapping("add")
    @ResponseBody
    public ResultInfo addBrand(Brand brand, HttpServletRequest request) {

        // 调用Service层的添加方法
        brandService.addBrand(brand);
        return success("品牌添加成功！");
    }


    /**
     * 修改品牌信息
     */
    @RequirePermission(code = "404004")
    @PostMapping("update")
    @ResponseBody
    public ResultInfo updateBrand(Brand brand) {
        // 调用Service层的修改方法
        brandService.updateBrand(brand);
        return success("品牌更新成功！");

    }


//删除品牌方
    @RequirePermission(code = "404005")
    @PostMapping("delete")
    @ResponseBody
    public ResultInfo deleteSaleChance(Integer[] ids) {
        // 调用Service层的删除方法
        brandService.deleteBatch(ids);
        return success("拉黑品牌方成功！");
    }





}
